<?php
namespace app\index\controller;
use think\Db;
class Connectthethird extends \think\Controller
{
    public function index()
    {
        return view();
    }
    public function getProvince()
    {
        $url = config("webService.url");
        $media_code = config("webService.mediaCode");
        $data = [
            "media_code"=>$media_code,
            "Interface_name"=>"Interface_province",
            "Conduct_type"=>1,
        ];
        $res = httpGet( $url."/GetINFOBYMEDIA?".http_build_query($data));

        return returnData(1,"获取省份信息成功",json_decode($res));
    }
    public function getCity()
    {
        $url = config("webService.url");
        $media_code = config("webService.mediaCode");
        $data = [
            "media_code"=>$media_code,
            "Interface_name"=>"Interface_city",
            "Conduct_type"=>1,
        ];
        $res = httpGet( $url."/GetINFOBYMEDIA?".http_build_query($data));

        return returnData(1,"获取城市信息成功",json_decode($res));
    }
    public function getCarModel()
    {
        $url = config("webService.url");
        $media_code = config("webService.mediaCode");
        $data = [
            "media_code"=>$media_code,
            "Interface_name"=>"Interface_modle",
            "Conduct_type"=>1,
        ];
        $res = httpGet( $url."/GetINFOBYMEDIA?".http_build_query($data));

        return returnData(1,"获取车型信息成功",json_decode($res));
    }
    public function getDealers()
    {
        $url = config("webService.url");
        $media_code = config("webService.mediaCode");
        $data = [
            "media_code"=>$media_code,
            "Interface_name"=>"Interface_dealers",
            "Conduct_type"=>1,
        ];
        $res = httpGet( $url."/GetINFOBYMEDIA?".http_build_query($data));

        return returnData(1,"获取经销商信息成功",json_decode($res));

    }
    public function getCarSeries()
    {
        $url = config("webService.url");
        $media_code = config("webService.mediaCode");
        $data = [
            "media_code"=>$media_code,
            "Interface_name"=>"Interface_series",
            "Conduct_type"=>1,
        ];
        $res = httpGet( $url."/GetINFOBYMEDIA?".http_build_query($data));
        return returnData(1,"获取车型配置成功",json_decode($res));

    }
    // 线索接口
    public function setSalesLeads()
    {
        $url = config("webService.debug")?config("webService.testUrl"):config("webService.url");//请求地址，根据debug判断
        $media_code = config("webService.mediaCode");

        $modle_code = input("model_code");
        $province_code = input("province_code");
        $province_name = input("province_name");
        $city_code = input("city_code");
        $city_name = input("city_name");
        $dealer_code = input("dealer_code");
        $dealer_name = input("dealer_name");
        $modle_code = input("modle_code");
        $modle_name = input("modle_name");
        $series_code = input("series_code");
        $series_name = input("series_name");
        $mobile = input("mobile");
        $customer_name = input("customer_name");
        $gender =  input("gender");
        //丰田后台数据格式
        $saleData = [
            [
            "media_code"=>$media_code,
            "clue_code"=>time().rand(0,10000),
            "is_publicorder"=> '2',
            "clue_type"=> '1',
            "province_code"=> $province_code,
            "province_name"=> $province_name,
            "city_code"=> $city_code,
            "city_name"=> $city_name,
            "dealer_code"=> $dealer_code,
            "dealer_name"=> $dealer_name,
            "modle_code"=> $modle_code,
            "modle_name"=> $modle_name,
            "series_code"=> $series_code,
            "series_name"=> $series_name,
            "mobile"=> $mobile,
            "customer_name"=>$customer_name,
            "gender"=>$gender,
            "addTime"=>time()
        ]];
        //写入自己数据库的信息
        $regData = [
            "customerName"=>$customer_name,
            "mobile"=>$mobile,
            "gender"=>$gender,
            "provinceCode"=>$province_code,
            "provinceName"=>$province_name,
            "cityCode"=>$city_code,
            "cityName"=>$city_name,
            "dealerCode"=>$dealer_code,
            "dealerName"=>$dealer_name,
            "modelCode"=>$modle_code,
            "modelName"=>$modle_name,
            "seriesCode"=>$series_code,
            "seriesName"=>$series_name,
            "mediaCode"=>$media_code,
            "addTime"=>time()
        ];

        $lastData = Db::table("register")->where("mobile",$mobile)->find();
        if($lastData){//如果以及登记过信息
            Db::table("register")->where("mobile",$mobile)->update($regData);
            $res=2;
        }else{//没有登记过信息
            Db::table("register")->insert($regData);
            //提交给丰田后台
            $data = [
                "media_code"=>$media_code,
                "Interface_name"=>"Interface_salesleads",
                "jsonstring"=>json_encode($saleData)
            ];
            $url = $url."/SetSalesLeads?".http_build_query($data);
            $res = httpGet($url);
            $res = preg_match("@true@is",$res);//含有true返回1，否则返回0
        }
        
		//$res=0为失败，1表示新增成功，2表示数据更新成功
        return returnData($res,$res?"提交成功！":"提交失败！");

    }
    //生成excel表格，已经把功能移到后台模块
    public function export_data()
    {
	$info_list = db('register')->select();		
        $this->assign([
            'export_consultant_list' => $info_list
        ]);
        header("Content-Type: application/vnd.ms-excel;charset=utf-8");
        header("Content-Disposition:filename=");
        header("Content-Disposition: attachment;filename=留资者信息.xls");
        header('Cache-Control: max-age=0');
        //die很重要，可以阻止页面渲染
        echo $this->fetch();
        die;
    }
}
